package io.github.maojian.map;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: jian.mao
 * @Date: 2022-06-29 09:40
 * @Description: https://leetcode.cn/problems/encode-and-decode-tinyurl/
 */
public class Q535 {
    private Map<Integer, String> map = new HashMap<>();
    private int id;

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        id++;
        map.put(id, longUrl);
        return "http://tinyurl.com/" + id;
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        int idIndex = shortUrl.lastIndexOf("/") + 1;
        int id = Integer.parseInt(shortUrl.substring(idIndex));
        return map.get(id);
    }

    @Test
    public void test() {
        String url = "https://leetcode.com/problems/design-tinyurl";
        String tiny = encode(url);
        System.out.println(tiny);
        System.out.println(decode(tiny));
    }
}
